# -*- coding:utf-8 -*-
"""
发送前一天的报表数据到指定会话
"""
import sys
import logging
from datetime import datetime, timedelta
from utils import tz
from utils.interface import send_text_msg
from settings import MG as mg


_LOGGER = logging.getLogger('bot')


def fill_report(day, index):
    now_str = '%s-%s' % (day.strftime('%Y-%m-%d'), index)
    now = mg.interval_report.find_one({
        '_id': now_str
    })
    ago_str = '%s-%s' % ((day - timedelta(1)).strftime('%Y-%m-%d'), index)
    ago = mg.interval_report.find_one({
        '_id': ago_str
    }) or {}
    _LOGGER.info('to fill report between %s and %s', now_str, ago_str)
    print now_str, ago_str
    if not now:
        _LOGGER.warn('now data not found, %s', now_str)
        return u'**暂无数据**'
    # 注册
    now_new_device = now['new_device']
    ago_new_device = ago.get('new_device', 0)
    delta = now_new_device - ago_new_device
    direction = u'↑' if delta >= 0 else u'↓'
    msg = u'**注册**:{} {}{}\n'.format(now_new_device, delta, direction)
    now_new_user = now['new_user']
    ago_new_user = ago.get('new_user', 0)
    delta = now_new_user - ago_new_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**注册绑定**:{} {}{}\n'.format(now_new_user, delta, direction)
    now_active_register_user = now['active_register_user']
    ago_active_register_user = ago.get('active_register_user', 0)
    delta = now_active_register_user - ago_active_register_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**绑定总数**:{} {}{}\n'.format(now_active_register_user, delta, direction)
    # 登录
    now_active_user = now['active_user']
    now_active_user_ios = now['active_user_ios']
    now_active_user_android = now['active_user_android']
    ago_active_user = ago.get('active_user', 0)
    ago_active_user_ios = ago.get('active_user_ios', 0)
    ago_active_user_android = ago.get('active_user_android', 0)
    delta = now_active_user - ago_active_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**今日登陆**:{} {}{}\n'.format(now_active_user, delta, direction)
    delta = now_active_user_ios - ago_active_user_ios
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**IOS登陆总数**:{} {}{}\n'.format(now_active_user_ios, delta, direction)
    delta = now_active_user_android - ago_active_user_android
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**Android登陆总数**:{} {}{}\n'.format(now_active_user_android, delta, direction)
    # 注充比
    now_new_recharge_device_ratio = now['new_recharge_device_ratio']
    ago_new_recharge_device_ratio = ago.get('new_recharge_device_ratio', 0)
    delta = now_new_recharge_device_ratio - ago_new_recharge_device_ratio
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**注充比例**:%.2f%% %.2f%%%s\n' % (
        now_new_recharge_device_ratio*100, delta*100, direction)
    # 注绑比
    now_new_register_device_ratio = now['new_register_device_ratio']
    ago_new_register_device_ratio = ago.get('new_register_device_ratio', 0)
    delta = now_new_register_device_ratio - ago_new_register_device_ratio
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**注绑比例**:%.2f%% %.2f%%%s\n' % (
        now_new_register_device_ratio*100, delta*100, direction)
    # 绑充比
    now_new_recharge_ratio = now['new_recharge_ratio']
    ago_new_recharge_ratio = ago.get('new_recharge_ratio', 0)
    delta = now_new_recharge_ratio - ago_new_recharge_ratio
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**绑充比例**:%.2f%% %.2f%%%s\n' % (
        now_new_recharge_ratio*100, delta*100, direction)
    msg += u'**今日新进入IP**:暂未开发\n'
    # 充值
    now_recharge_price = now['recharge_price']
    ago_recharge_price = ago.get('recharge_price', 0)
    delta = now_recharge_price - ago_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**充值总数**:{} {}{}\n'.format(now_recharge_price, delta, direction)
    now_new_recharge_price = now['new_recharge_price']
    ago_new_recharge_price = ago.get('new_recharge_price', 0)
    delta = now_new_recharge_price - ago_new_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**新用户充值**:{} {}{}\n'.format(now_new_recharge_price, delta, direction)
    now_old_recharge_price = now['old_recharge_price']
    ago_old_recharge_price = ago.get('old_recharge_price', 0)
    delta = now_old_recharge_price - ago_old_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**老用户充值**:{} {}{}\n'.format(now_old_recharge_price, delta, direction)
    now_online_recharge_price = now['online_recharge_price']
    ago_online_recharge_price = ago.get('online_recharge_price', 0)
    delta = now_online_recharge_price - ago_online_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**在线充值**:{} {}{}\n'.format(now_online_recharge_price, delta, direction)
    now_new_online_recharge_price = now['new_online_recharge_price']
    ago_new_online_recharge_price = ago.get('new_online_recharge_price', 0)
    delta = now_new_online_recharge_price - ago_new_online_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**新用户在线充值**:{} {}{}\n'.format(now_new_online_recharge_price, delta, direction)
    now_old_online_recharge_price = now['old_online_recharge_price']
    ago_old_online_recharge_price = ago.get('old_online_recharge_price', 0)
    delta = now_old_online_recharge_price - ago_old_online_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**老用户在线充值**:{} {}{}\n'.format(now_old_online_recharge_price, delta, direction)
    now_agent_recharge_price = now['agent_recharge_price']
    ago_agent_recharge_price = ago.get('agent_recharge_price', 0)
    delta = now_agent_recharge_price - ago_agent_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**代理实际销售**:{} {}{}\n'.format(now_agent_recharge_price, delta, direction)
    msg += u'**代理充值**:{} {}{}\n'.format(now_agent_recharge_price, delta, direction)
    now_new_agent_recharge_price = now['new_agent_recharge_price']
    ago_new_agent_recharge_price = ago.get('new_agent_recharge_price', 0)
    delta = now_new_agent_recharge_price - ago_new_agent_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**新用户代理充值**:{} {}{}\n'.format(now_new_agent_recharge_price, delta, direction)
    now_old_agent_recharge_price = now['old_agent_recharge_price']
    ago_old_agent_recharge_price = ago.get('old_agent_recharge_price', 0)
    delta = now_old_agent_recharge_price - ago_old_agent_recharge_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**老用户代理充值**:{} {}{}\n'.format(now_old_agent_recharge_price, delta, direction)
    now_recharge_user = now['recharge_user']
    ago_recharge_user = ago.get('recharge_user', 0)
    delta = now_recharge_user - ago_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**充值人数**:{} {}{}\n'.format(now_recharge_user, delta, direction)
    now_new_recharge_user = now['new_recharge_user']
    ago_new_recharge_user = ago.get('new_recharge_user', 0)
    delta = now_new_recharge_user - ago_new_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**新用户充值人数**:{} {}{}\n'.format(now_new_recharge_user, delta, direction)
    now_old_recharge_user = now['old_recharge_user']
    ago_old_recharge_user = ago.get('old_recharge_user', 0)
    delta = now_old_recharge_user - ago_old_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**老用户充值人数**:{} {}{}\n'.format(now_old_recharge_user, delta, direction)
    now_online_recharge_user = now['online_recharge_user']
    ago_online_recharge_user = ago.get('online_recharge_user', 0)
    delta = now_online_recharge_user - ago_online_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**在线充值人数**:{} {}{}\n'.format(now_online_recharge_user, delta, direction)
    now_new_online_recharge_user = now['new_online_recharge_user']
    ago_new_online_recharge_user = ago.get('new_online_recharge_user', 0)
    delta = now_new_online_recharge_user - ago_new_online_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**新用户在线充值人数**:{} {}{}\n'.format(now_new_online_recharge_user, delta, direction)
    now_old_online_recharge_user = now['old_online_recharge_user']
    ago_old_online_recharge_user = ago.get('old_online_recharge_user', 0)
    delta = now_old_online_recharge_user - ago_old_online_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**老用户在线充值人数**:{} {}{}\n'.format(now_old_online_recharge_user, delta, direction)
    now_agent_recharge_user = now['agent_recharge_user']
    ago_agent_recharge_user = ago.get('agent_recharge_user', 0)
    delta = now_agent_recharge_user - ago_agent_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**代理充值人数**:{} {}{}\n'.format(now_agent_recharge_user, delta, direction)
    now_new_agent_recharge_user = now['new_agent_recharge_user']
    ago_new_agent_recharge_user = ago.get('new_agent_recharge_user', 0)
    delta = now_new_agent_recharge_user - ago_new_agent_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**新用户代理充值人数**:{} {}{}\n'.format(now_new_agent_recharge_user, delta, direction)
    now_old_agent_recharge_user = now['old_agent_recharge_user']
    ago_old_agent_recharge_user = ago.get('old_agent_recharge_user', 0)
    delta = now_old_agent_recharge_user - ago_old_agent_recharge_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**老用户代理充值人数**:{} {}{}\n'.format(now_old_agent_recharge_user, delta, direction)
    now_recharge_count = now['recharge_count']
    ago_recharge_count = ago.get('recharge_count', 0)
    delta = now_recharge_count - ago_recharge_count
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**充值笔数**:{} {}{}\n'.format(now_recharge_count, delta, direction)
    now_online_recharge_count = now['online_recharge_count']
    ago_online_recharge_count = ago.get('online_recharge_count', 0)
    delta = now_online_recharge_count - ago_online_recharge_count
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**在线充值笔数**:{} {}{}\n'.format(now_online_recharge_count, delta, direction)
    now_agent_recharge_count = now['agent_recharge_count']
    ago_agent_recharge_count = ago.get('agent_recharge_count', 0)
    delta = now_agent_recharge_count - ago_agent_recharge_count
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**代理充值笔数**:{} {}{}\n'.format(now_agent_recharge_count, delta, direction)
    now_agent_import = now['agent_import']
    ago_agent_import = ago.get('agent_import', 0)
    delta = now_agent_import - ago_agent_import
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**代理进货**:{} {}{}\n'.format(now_agent_import, delta, direction)
    now_agent_cost = now['agent_cost']
    ago_agent_cost = ago.get('agent_cost', 0)
    delta = now_agent_cost - ago_agent_cost
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**代理充值金额**:{} {}{}\n'.format(now_agent_cost, delta, direction)
    now_agent_balance = now['agent_balance']
    ago_agent_balance = ago.get('agent_balance', 0)
    delta = now_agent_balance - ago_agent_balance
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**代理充值余币**:{} {}{}\n'.format(now_agent_balance, delta, direction)
    now_average_recharge = now['average_recharge']
    ago_average_recharge = ago.get('average_recharge', 0)
    delta = now_average_recharge - ago_average_recharge
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**人均充值**:%.2f %.2f%s\n' % (now_average_recharge, delta, direction)
    now_recharge_rate = now['recharge_rate']
    ago_recharge_rate = ago.get('recharge_rate', 0)
    delta = now_recharge_rate - ago_recharge_rate
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**付费率**:%.2f%% %.2f%%%s\n' % (now_recharge_rate*100, delta*100, direction)
    now_average_contribute = now['average_contribute']
    ago_average_contribute = ago.get('average_contribute', 0)
    delta = now_average_contribute - ago_average_contribute
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**人均贡献**:%.2f %.2f%s\n' % (now_average_contribute, delta, direction)
    now_withdraw_price = now['withdraw_price']
    ago_withdraw_price = ago.get('withdraw_price', 0)
    delta = now_withdraw_price - ago_withdraw_price
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**兑换**:{} {}{}\n'.format(now_withdraw_price, delta, direction)
    now_withdraw_count = now['withdraw_count']
    ago_withdraw_count = ago.get('withdraw_count', 0)
    delta = now_withdraw_count - ago_withdraw_count
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**兑换人数**:{} {}{}\n'.format(now_withdraw_count, delta, direction)
    now_play_user = now['play_user']
    ago_play_user = ago.get('play_user', 0)
    delta = now_play_user - ago_play_user
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**活跃人数**:{} {}{}\n'.format(now_play_user, delta, direction)
    now_profit_rate = now['real_profit_rate']
    ago_profit_rate = ago.get('real_profit_rate', 0)
    delta = now_profit_rate - ago_profit_rate
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**营收比**:%.2f%% %.2f%%%s\n' % (now_profit_rate*100, delta*100, direction)
    now_profit = now['real_profit']
    ago_profit = ago.get('real_profit', 0)
    delta = now_profit - ago_profit
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**营收**:{} {}{}\n'.format(now_profit, delta, direction)
    now_user_stay_2 = now['user_stay_2']
    ago_user_stay_2 = ago.get('user_stay_2', 0)
    delta = now_user_stay_2 - ago_user_stay_2
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**昨日留存**:%.2f%% %.2f%%%s\n' % (
        now_user_stay_2*100, delta*100, direction)
    now_user_stay_3 = now['user_stay_3']
    ago_user_stay_3 = ago.get('user_stay_3', 0)
    delta = now_user_stay_3 - ago_user_stay_3
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**3日留存**:%.2f%% %.2f%%%s\n' % (
        now_user_stay_3*100, delta*100, direction)
    now_user_stay_7 = now['user_stay_7']
    ago_user_stay_7 = ago.get('user_stay_7', 0)
    delta = now_user_stay_7 - ago_user_stay_7
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**7日留存**:%.2f%% %.2f%%%s\n' % (
        now_user_stay_7*100, delta*100, direction)
    # life time value
    ltv7 = now.get('ltv7', 0)
    msg += u'**7天LTV值**:%.2f\n' % ltv7
    ltv14 = now.get('ltv14', 0)
    msg += u'**14天LTV值**:%.2f\n' % ltv14
    ltv30 = now.get('ltv30', 0)
    msg += u'**30天LTV值**:%.2f\n' % ltv30
    ltv60 = now.get('ltv60', 0)
    msg += u'**60天LTV值**:%.2f\n' % ltv60

    now_arpu = now['arpu']
    ago_arpu = ago.get('arpu', 0)
    delta = now_arpu - ago_arpu
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**日ARPU**:%.2f %.2f%s\n' % (now_arpu, delta, direction)
    now_arppu = now['arppu']
    ago_arppu = ago.get('arppu', 0)
    delta = now_arppu - ago_arppu
    direction = u'↑' if delta >= 0 else u'↓'
    msg += u'**日ARPPU**:%.2f %.2f%s\n' % (now_arppu, delta, direction)
    return msg


def current_report():
    """
    根据当前时间生成当时的报表
    """
    now = tz.local_now()
    day = now
    if now.hour == 0:
        day = day - timedelta(days=1)
    index = now.hour - 1 if now.hour>0 else 23
    header_msg = u'**项目**:腾飞科技二部-BSCP\n'
    header_msg += u'*今日同时段数据 / 与昨日同时段对比*\n'
    report_msg = fill_report(day, index)
    return header_msg + report_msg


def history_report(delta):
    """
    生成历史日期的数据
    delta: 和当天日期的差值, 1即为1天前
    """
    day = tz.local_now() - timedelta(days=delta)
    header_msg = u'**项目**:腾飞科技二部-BSCP\n'
    if delta == 1:
        header_msg += u'*昨日数据统计 [与前日对比]*\n'
    else:
        now_date = day.strftime('%Y-%m-%d')
        header_msg += u'*{}数据统计 [与前日对比]*\n'.format(now_date)
    report_msg = fill_report(day, 23)
    return header_msg + report_msg


if __name__ == "__main__":
    chat_type = int(sys.argv[1])
    chat_id = int(sys.argv[2])
    #report_msg = current_report()
    report_msg = history_report(1)
    send_text_msg(chat_type, chat_id, report_msg)
